package com.ruoyi.system.domain;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.enums.comlbs.OrderSendStatusEnum;
import com.ruoyi.system.domain.typehandler.CmdConfigTypeHandler;
import com.ruoyi.system.domain.vo.order.OrderConfig;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * 指令发送记录对象 t_order_send_record
 * 
 * @author wp
 * @date 2023-08-10
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
@Accessors(chain = true)
@TableName(value = "t_order_send_record", autoResultMap = true, excludeProperty = "remark")
public class OrderSendRecord extends BaseEntity {

    @TableId(type = IdType.ASSIGN_ID)
    private Long id;
    @ApiModelProperty("指令ID")
    private Long orderId;
    @ApiModelProperty("车辆id")
    private Long vehicleId;
    @ApiModelProperty("设备名称")
    private String vehicleNum;
    @ApiModelProperty("设备号")
    private String deviceNo;
    @ApiModelProperty("指令参数json字符串")
    @TableField(typeHandler = JacksonTypeHandler.class)
    private JSONObject content;
    @ApiModelProperty("配置值json字符串")
    @TableField(typeHandler = CmdConfigTypeHandler.class)
    private List<OrderConfig> confValue;
    @ApiModelProperty("消息id,标志消息类型")
    private Long msgId;
    @ApiModelProperty("状态(0：待下发，1：发送成功，2：发送失败，3：通用应答成功，4：特殊应答成功，5：配置失败)")
    private Integer status;
    @TableField(exist = false)
    private String statusName;
    @ApiModelProperty("失败原因")
    private String failReason;
    @ApiModelProperty("应答结果json")
    private String responseResult;
    @ApiModelProperty("应答结果内容")
    private String responseValue;
    @ApiModelProperty("分组id")
    private Long groupId;

    /** 以下为非数据库字段 **/
    //创建用户名称
    @TableField(exist = false)
    private String createUserName;
    @ApiModelProperty("指令名称(多个用逗号隔开)")
    @TableField(exist = false)
    private String orderName;
    @ApiModelProperty("指令类型")
    @TableField(exist = false)
    private String orderType;
    @JsonIgnore
    @ApiModelProperty("开始时间")
    @TableField(exist = false)
    private String startTime;
    @JsonIgnore
    @ApiModelProperty("结束时间")
    @TableField(exist = false)
    private String endTime;

    /**
     * 指令是否发送成功
     * @return
     */
    public boolean isSendSuccess(){
        return status != null && (status.equals(OrderSendStatusEnum.COMMON_REPLY_SUCCESS.getCode())
                || status.equals(OrderSendStatusEnum.SPECIAL_REPLY_SUCCESS.getCode()));
    }
}
